﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using QT.Controls;
using QT.Windows.Shell;

namespace SVNClear
{
    public partial class SVNClear : GlassForm
    {
        #region Attributes

        private int count = 0;
        private string Path = "";

        Color color = Color.FromArgb(164, 212, 211);

        #endregion

        #region SVNClear

        public SVNClear()
        {
            InitializeComponent();
        }

        private void SVNClear_Load(object sender, EventArgs e)
        {
            Path = Application.StartupPath;
        }

        #endregion

        #region Event

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.folderBrowserDialog1.SelectedPath = Path;
            this.folderBrowserDialog1.Description = "请选择要清理的目录，默认为程序运行目录";
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            Path = this.folderBrowserDialog1.SelectedPath;

            if (!Directory.Exists(Path))
            {
                MessageBox.Show("系统错误");
                Path = Application.StartupPath;
                return;
            }

            DirectoryInfo DirInfo = new DirectoryInfo(Path);
            DirectoryClear(DirInfo);

            MessageBox.Show("清理完成");
            this.lblProcess.Text = "清理完成。共清理.svn目录" + count + "个。";
        }

        #endregion

        #region Method

        private void DirectoryClear(DirectoryInfo DirInfo)
        {
            DirectoryInfo[] Directories = DirInfo.GetDirectories();

            foreach (DirectoryInfo Dir in Directories)
            {
                if (Dir.Name.ToLower() == ".svn")
                {
                    SetAttribute(Dir);
                    Directory.Delete(Dir.FullName, true);
                    count++;
                    this.lblProcess.Text = "正在删除："+Dir.FullName;
                }
                else DirectoryClear(Dir);
            }
        }

        private void SetAttribute(DirectoryInfo DirInfo)
        {
            FileInfo[] Files = DirInfo.GetFiles();

            foreach (FileInfo file in Files)
            {
                file.IsReadOnly = false;
            }

            DirectoryInfo[] Directories = DirInfo.GetDirectories();

            foreach (DirectoryInfo dir in Directories)
            {
                SetAttribute(dir);
            }
        }

        #endregion        
    }
}
